package cn.brilliantfuture.commentry.refund.impl;

import java.util.Date;
import java.util.List;

import javax.ejb.Stateless;
import javax.jws.WebService;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.log4j.Logger;
import org.jboss.wsf.spi.annotation.WebContext;

import cn.brilliantfuture.commentry.entity.DeliveryPerson;
import cn.brilliantfuture.commentry.entity.RefundOrderInfo;
import cn.brilliantfuture.commentry.entity.RefundPickRecords;
import cn.brilliantfuture.commentry.entity.StorageGridPurchaserNews;
import cn.brilliantfuture.commentry.refund.RefundPickMessageRemote;
import cn.brilliantfuture.commentry.refund.RefundPickMessageWebService;

/**
 * 
 * Title: Communication Entry
 * 
 * Author: LiTian
 * 
 * Date: 2011-10-25
 * 
 * Description: 退货取货流程
 * 
 */
@Stateless
@WebService(endpointInterface = "cn.brilliantfuture.commentry.refund.RefundPickMessageWebService")
@WebContext(contextRoot = "/commentry", urlPattern = "/RefundPickMessageWebService")
public class RefundPickMessageImpl implements RefundPickMessageRemote,
		RefundPickMessageWebService {
	private static Logger logger = Logger.getLogger(RefundPickMessageImpl.class);
	
	private final int TAKE_GOODS_OK = 1;
	private final int TAKE_GOODS_FAIL = 0;

	@PersistenceContext(unitName = "commentry-jpa")
	private EntityManager entityManager;

	@SuppressWarnings("unchecked")
	public int refundPickWorkflow(String deviceSn, int gridNum, String cardNum, String rfidSn, String timestamp) {
		logger.info("RefundPickMessageImpl.refundPickWorkflow(String deviceSn, int gridNo, String cardNum, String rfidSn, String timestamp) ...");
		logger.info("deviceSN -> " + deviceSn + " gridNum -> " + gridNum + " cardNum -> " + cardNum + " rfidSn -> " + rfidSn + " timestamp -> " + timestamp);
		
		Date currentDate = new Date();
		
		List<DeliveryPerson> deliveryPersons = entityManager.createQuery("select dp from DeliveryPerson dp where dp.number = :cardNum").setParameter("cardNum", cardNum).getResultList();
		
		if (deliveryPersons.size() == 0) {
			return TAKE_GOODS_FAIL;
		}
		
		Query query  = entityManager.createQuery("select sgpn from StorageGridPurchaserNews sgpn where sgpn.closing = 0 and sgpn.pick = 0 and sgpn.refund = 1 and sgpn.storageGrid.gridNum = :gridNum and sgpn.deviceSn = :deviceSn and sgpn.rfidSn = :rfidSn");
		List<StorageGridPurchaserNews> sgpns = query.setParameter("gridNum", gridNum).setParameter("deviceSn", deviceSn).setParameter("rfidSn", rfidSn).getResultList();
			
		if (sgpns.size() > 0){
			StorageGridPurchaserNews sgpn = (StorageGridPurchaserNews) sgpns.get(0);
			sgpn.setClosing(1);
			sgpn.setPick(1);
			sgpn.setTakeawayTime(currentDate);
			entityManager.merge(sgpn);
			
			RefundPickRecords refundPickRecords = new RefundPickRecords();		
			refundPickRecords.setCardNumber(cardNum);
			refundPickRecords.setGridNumber(gridNum);
			refundPickRecords.setFlag(false);
			refundPickRecords.setOrderInfoNumber(sgpn.getOrderInformationNumber());
			refundPickRecords.setRfidSn(rfidSn);
			refundPickRecords.setPickTime(currentDate);
			entityManager.persist(refundPickRecords);
			
			entityManager.flush();
			
			Object count = entityManager.createQuery("select count(pb) from ProductBatch pb where pb.orderInformation.number = :number").setParameter("number", sgpn.getOrderInformationNumber()).getSingleResult();
			Object done = entityManager.createQuery("select count(sgpn) from StorageGridPurchaserNews sgpn where sgpn.refund = 1 and sgpn.pick = 1 and sgpn.orderInformationNumber = :number").setParameter("number", sgpn.getOrderInformationNumber()).getSingleResult();
			
			if (Integer.parseInt(count.toString()) == Integer.parseInt(done.toString())){

				List<RefundOrderInfo> refundOrderInfos = entityManager.createQuery("select roi from RefundOrderInfo roi where roi.orderInfoNumber = :number").setParameter("number", sgpn.getOrderInformationNumber()).getResultList();
				
				RefundOrderInfo refundOrderInfo = refundOrderInfos.get(0);
				
				refundOrderInfo.setDeliveryPerson(deliveryPersons.get(0));
				
				refundOrderInfo.setPickTime(currentDate);
				refundOrderInfo.setTransactions(3);
				entityManager.merge(refundOrderInfo);
				entityManager.flush();
			}
			
			return TAKE_GOODS_OK;
		}
		
		return TAKE_GOODS_FAIL;
	}

}
